var express = require('express');
var router = express.Router();// 路由
const { get, add, amend, getOne, getsearch, upData, delImg, Delete, addAddr, getAddr } = require("../api/address")


// 获取数据get类型 和页面相关的操作
router.get('/', async function (req, res, _next) {
    // 解构当前页和每页显示条数
    const { curPage, eachPage } = req.query;
    const data = await get({ curPage, eachPage });
    res.send(data);
});

// 新增驿站
router.post('/add', async function (req, res, _next) {
    const { name, address, phone, Open_hours, longitude, latitude, imgs, _distance } = req.body;
    const data = await add({ name, address, phone, Open_hours, longitude, latitude, imgs, _distance })
    res.send(data)
});

// 删除数据
router.delete("/", async function (req, res, next) {
    const { _id } = req.body;
    const data = await Delete(_id);
    res.send(data)
});
// 修改数据
router.patch('/amend', async function (req, res, _next) {
    // 请求体获取到到参数
    const { name, address, phone, Open_hours, longitude, latitude, _id } = req.body;
    const data = await amend({ name, address, phone, Open_hours, longitude, latitude, _id })
    res.send(data);
})

// 查询一条数据
router.get('/getone', async function (req, res, _next) {
    const { _id } = req.query;
    const data = await getOne(_id)
    res.send(data);

})

// 搜索数据
router.get('/getsearch', async function (req, res, _next) {
    const { name } = req.query;
    const data = await getsearch(name)
    res.send(data);
})

// 新增图片
router.patch('/:_id', async function (req, res, _next) {
    const { _id } = req.params;
    const data = req.body;
    await upData({ _id }, data)
    const result = await getOne(_id)
    res.send(result);
})

// 删除图片
router.delete("/:_id/address/:url", async function (req, res, _next) {
    //params路径当解析
    const { _id, url } = req.params;
    const data = await delImg({ _id, url });
    res.send(data);
});

// 新增一条并别删除其他数据
router.post('/addAddr', async function (req, res, _next) {
    const { name, address, phone, Open_hours, longitude, latitude, imgs, _distance } = req.body;
    const data = await addAddr({ name, address, phone, Open_hours, longitude, latitude, imgs, _distance })
    res.send(data)
});


//uniapp 获取当前提货点
router.get('/getAddr', async function (req, res, _next) {
    const data = await getAddr()
    res.send(data);

})


module.exports = router;
